package extensions.prototipador.undo;

import java.util.ArrayList;
import java.util.List;

import extensions.prototipador.Prototype;
import extensions.prototipador.sprites.Sprite;

public class UndoTool
{
	private Prototype _prototype;
	private List<UndoAction> _actions;
	public UndoTool(Prototype prototype)
	{
		_prototype = prototype;
		_actions = new ArrayList<UndoAction>();
	}
	
	public void add(UndoAction a)
	{
		_actions.add(a);
	}
	
	public boolean undo()
	{
		if (_actions.size() == 0)
			return false;
		
		UndoAction action = _actions.remove(_actions.size()-1);
		switch (action._actionType)
		{
    		case Create:
    			_prototype.removeAll(action._sprites);
    			break;
    		case Move:
    			
    			for (Sprite s : action._sprites)
    				s.setPosition(action._x, action._y);
    			break;
    		case Delete:
    			_prototype.addAll(action._sprites);
    			break;
		}
		
		return true;
	}
}